//
//  LCVideoDetailChangeHeaderController+LCPingLun.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/20.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCVideoDetailChangeHeaderController+LCPingLun.h"
#import "LCVideoHomeListAboutModel.h"

@implementation LCVideoDetailChangeHeaderController (LCPingLun)

#pragma mark - 
- (void)initWithAppraise:(UIView *)view share:(NSInteger)share :(videoStrPinglLun)block {
    [[YYKeyboardManager defaultManager] addObserver:self];

    UIView *lowView = [[UIView alloc] init];
    lowView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lowView];
    [lowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.height.mas_equalTo(50);
    }];
    UIView *viewBackColor = [[UIView alloc] init];
    [viewBackColor.layer setMasksToBounds:YES];
    [viewBackColor.layer setCornerRadius:16.5];
    viewBackColor.backgroundColor = RGBColor(243, 243, 243);
    [lowView addSubview:viewBackColor];
    
    [viewBackColor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lowView.mas_left).offset(15);
        make.right.equalTo(lowView.mas_right).offset(-60);
        make.bottom.equalTo(lowView.mas_bottom).offset(-8);
        make.top.equalTo(lowView.mas_top).offset(7.5);
    }];
    
    UIImageView *imageViewBi = [[UIImageView alloc] init];
    imageViewBi.image = [UIImage imageNamed:@"编辑"];
    [viewBackColor addSubview:imageViewBi];
    
    [imageViewBi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(15);
        //        make.right.equalTo(viewBackColor.mas_right).offset(-60);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(-6.5);
        make.top.equalTo(viewBackColor.mas_top).offset(8.5);
        //        make.height.mas_equalTo(@15);
    }];
    
    UITextView *textView = [[UITextView alloc] init];
    //    textView. = @"点击此处说两句...";
    //    textView.scrollEnabled = NO;
    textView.delegate = self;
    textView.scrollsToTop = NO;
    textView.font = [UIFont systemFontOfSize:14];
    textView.backgroundColor = RGBColor(243, 243, 243);
    //当textview的字符串为0时发送（rerurn）键无效
    textView.enablesReturnKeyAutomatically = YES;
    textView.keyboardType = UIKeyboardTypeDefault;
    //键盘return样式变成发送
    textView.returnKeyType = UIReturnKeySend;
    [viewBackColor addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewBackColor.mas_left).offset(35);
        make.right.equalTo(viewBackColor.mas_right).offset(-10);
        make.bottom.equalTo(viewBackColor.mas_bottom).offset(0);
        make.top.equalTo(viewBackColor.mas_top).offset(0);
        //        make.height.mas_equalTo(@15);
    }];
    UILabel *textbguilabel = [[UILabel alloc] init];
    textbguilabel.text = @"写评论....";
    textbguilabel.enabled = NO;//lable必须设置为不可用
    textbguilabel.font = [UIFont systemFontOfSize:13];
    textbguilabel.backgroundColor = [UIColor clearColor];
    [textView addSubview:textbguilabel];
    //WithFrame:CGRectMake(2,3, textView.frame.size.width, 20
    [textbguilabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView.mas_left).offset(3);
        make.right.equalTo(textView.mas_right).offset(-0);
        make.bottom.equalTo(textView.mas_bottom).offset(-10);
        make.top.equalTo(textView.mas_top).offset(10);
        //        make.height.mas_equalTo(@15);
    }];
    
    UIButton *shareBut = [[UIButton alloc] init];
    [lowView addSubview:shareBut];
    [shareBut setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [shareBut setTitle:@"发送" forState:(UIControlStateNormal)];
    if (share == 1) {
        shareBut.hidden = NO;
    }else{
        shareBut.hidden = YES;
    }
    [lowView addSubview:shareBut];
    [shareBut addTarget:self action:@selector(senderUserCommentsOnResources) forControlEvents:UIControlEventTouchUpInside];

    [shareBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lowView.mas_right).offset(-5);
        make.bottom.equalTo(lowView.mas_bottom).offset(0);
        make.top.equalTo(lowView.mas_top).offset(0);
        make.width.mas_equalTo(@48);
    }];
    
    block(lowView,textView,textbguilabel,shareBut);
}
- (void)pushLCLoginVC {
    pushAccLoginVC;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *strLong = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (strLong.length == 0) {
        self.textbguilabel.hidden = NO;
    }else{
        if (strLong.length > 0 &&self.textbguilabel.hidden == NO) {
            self.textbguilabel.hidden = YES;
        }
    }
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        NSLog(@"点击发送键");
        [self senderUserCommentsOnResources];
        [self.view endEditing:YES];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    return YES;
}
#pragma mark - 进行评价
- (void)senderUserCommentsOnResources {
    NSString *content = self.textView.text;
    if (content.length  == 0) {
        return;
    }
        
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    NSString *resource_id;
    resource_id = self.listAboutModel.id;
    ///监听键盘事件,回车进行发送评论
    __weak typeof(self) weakSelf = self;
    [LSZRequestData UserCommentsOnResources:resource_id content:content type:@"2" :^(BOOL isOK, NSString * Str, NSDictionary * dic) {
        if (isOK == YES) {
            //评论完清空掉文本框
            weakSelf.textView.text = @"";
            weakSelf.textbguilabel.hidden = NO;
            [weakSelf.view endEditing:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                weakSelf.videoPingLunTableViewController.page = 1;
                [weakSelf.videoPingLunTableViewController getVideoCommentsList:1];
                [weakSelf.videoPingLunTableViewController.tableView reloadData];
            });
            [SVProgressHUD showSuccessWithStatus:@"评论成功!"];
            [SVProgressHUD dismissWithDelay:1];
        }else{
            [SVProgressHUD showErrorWithStatus:Str];
            [SVProgressHUD dismissWithDelay:1];
        }
    }];
}


#pragma mark -  评论  -
- (void)keyboardChangedWithTransition:(YYKeyboardTransition)transition {
    CGRect frame = [YYKeyboardManager defaultManager].keyboardFrame;
    frame = [[YYKeyboardManager defaultManager] convertRect:frame toView:self.view];
    // 获取键盘当前状态
    BOOL visible = [YYKeyboardManager defaultManager].keyboardVisible;
    
    if (visible) {//键盘展开
        self.textbguilabel.text = @"请输入内容";
        [self.lowView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            if (@available(iOS 11, *)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-frame.size.height);
            }else{
                make.bottom.equalTo(self.view.mas_bottom).offset(-frame.size.height);
            }
            make.height.mas_equalTo(50);
        }];
    }else{//键盘收回
        self.textbguilabel.text = @"写评论";
        [self.lowView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            if (@available(iOS 11, *)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }else{
                make.bottom.equalTo(self.view.mas_bottom);
            }
            make.height.mas_equalTo(50);
        }];
    }
}

@end
